{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab9e2da3",
   "metadata": {},
   "source": [
    "## asr优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c7c7a39a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f2a9b481",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'ns2A03go6t26n8GNBgKGvcbp'\n",
    "SECRET_KEY = 'utz7RWIG1cjoFaA6d7ZIyqokndt7zd19'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5cf22ee7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.57a029fb50ea5bbb3f5424dff8e38c88.2592000.1655296934.282335-26192257'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xiaoma_token=fetch_token(API_KEY,SECRET_KEY)\n",
    "xiaoma_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6664b2b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "#将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b7819f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'快递也可以上去是吧？今天天蝎座的运势。'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = eval(asr(token = xiaoma_token,AUDIO_FILE = '1.wav'))['result'][0]\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b849d03",
   "metadata": {},
   "source": [
    "## 图灵机器人API测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3c34e831",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e7e1447",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "2afaf6e2",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-32-bd15239caf63>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-32-bd15239caf63>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    TL_key =\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def TL_API(TL_key,text):\n",
    "    TL_url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "    payload = {\n",
    "        \"reqType\":0,\n",
    "        \"perception\":{\n",
    "            \"text\":\"你好\"\n",
    "        },\n",
    "        \"userInfo\":{\n",
    "            \"apikey\":TL_key,\n",
    "            \"userId\":\"0000001\"\n",
    "        }\n",
    "    }\n",
    "    res = requests.post(TL_url,headers=headers,data=json.dumps(payload)\n",
    "    return res.json()['results'][0]['values']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4755f809",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ef6361d",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c6317df",
   "metadata": {},
   "source": [
    "## 有图灵接口的语音识别对话机器人\n",
    "* 目标：我说话，机器人回复文本信息（YULING_API）  \n",
    "> 1.准备音频（临时文件-时间期限）  \n",
    "> 2.调用百度语音识别——文本信息（识别到的内容）  \n",
    "> 3.调用图灵机器人——文本信息（智能返回的内容）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "beab16b6",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'TL_API' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-f961458c6632>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;31m# 3.调用图灵机器人\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mTL_API\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mTL_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0masr_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'TL_API' is not defined"
     ]
    }
   ],
   "source": [
    "# 1.准备音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "\n",
    "#将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f:\n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "\n",
    "# 2.调用百度\n",
    "xiaoma_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "asr_result = eval(asr(token = xiaoma_token,AUDIO_FILE = '1.wav'))['result'][0]\n",
    "\n",
    "# 3.调用图灵机器人\n",
    "TL_API(TL_key,asr_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36d7079e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
